package com.zaizai.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zaizai.reggie.common.R;
import com.zaizai.reggie.entity.Category;
import com.zaizai.reggie.entity.Employee;
import com.zaizai.reggie.service.CategoryService;
import com.zaizai.reggie.service.impl.CategoryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RequestMapping("/category")
@RestController
public class CategoryController {
    @Autowired
    public CategoryServiceImpl categoryService;


    @PostMapping
    public R<String> save(@RequestBody Category category){


        categoryService.save(category);
        return R.success("新增分类成功成功");
    }


    @GetMapping("/page")
    public R<Page<Category>> page(int page, int pageSize) {
        //构造分页构造器
        Page pageInfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.orderByAsc(Category::getSort);
        //执行查询
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }
    @DeleteMapping
    public R<String> removeById(Long ids) {
        categoryService.remove(ids);
        return R.success("删除成功");
    }


    @PutMapping()
    public R<String> update(@RequestBody Category category){


        categoryService.updateById(category);
        return R.success("修改成功");

    }
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> LambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper.eq(category.getType()!=null, Category::getType, category.getType());
        LambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(LambdaQueryWrapper);
        return R.success(list);


    }
}
